﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UmlDesigner.Gui
{
    internal partial class ResizeDocumentForm : Form
    {
        public ResizeDocumentForm(UmlControl control)
        {
            InitializeComponent();

            targetControl = control;
            unitsComboBox.SelectedIndex = 0;
        }

        UmlControl targetControl;

        private void button1_Click(object sender, EventArgs e)
        {
            int w, h;

            if (PercentSelected())
            {
                // Percent
                w = (targetControl.currentDocument.DocumentSize.Width * (int)widthNumericUpDown.Value) / 100;
                h = (targetControl.currentDocument.DocumentSize.Height * (int)heightNumericUpDown.Value) / 100;
            }
            else
            {
                // Pixels
                w = ((int)widthNumericUpDown.Value);
                h = ((int)heightNumericUpDown.Value);
            }

            targetControl.ResizeDocument(new Size(w, h));
        }

        private int GetWidthAsPercent()
        {
            if (PercentSelected())
            {
                // Percent
                return (int)widthNumericUpDown.Value;
            }
            else
            {
                // Pixels
                return ((int)widthNumericUpDown.Value * 100 / targetControl.currentDocument.DocumentSize.Width);
            }
        }

        private int GetHeightAsPercent()
        {
            if (PercentSelected())
            {
                // Percent
                return (int)heightNumericUpDown.Value;
            }
            else
            {
                // Pixels
                return ((int)heightNumericUpDown.Value * 100 / targetControl.currentDocument.DocumentSize.Height);
            }
        }

        private bool PercentSelected()
        {
            return unitsComboBox.SelectedIndex == 0;
        }

        private int oldIndex = 0;

        private void unitsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PercentSelected() && oldIndex != 0)
            {
                // Percent (was pixels)
                widthNumericUpDown.Value = (widthNumericUpDown.Value * 100 / targetControl.currentDocument.DocumentSize.Width);
                heightNumericUpDown.Value = (heightNumericUpDown.Value * 100 / targetControl.currentDocument.DocumentSize.Height);
            }
            else if (!PercentSelected() && oldIndex != 1)
            {
                // Pixels (was percent)
                widthNumericUpDown.Value = (targetControl.currentDocument.DocumentSize.Width * widthNumericUpDown.Value) / 100;
                heightNumericUpDown.Value = (targetControl.currentDocument.DocumentSize.Height * heightNumericUpDown.Value) / 100;
            }
            oldIndex = unitsComboBox.SelectedIndex;
        }

        private void widthNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
        }

        private void heightNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
        }

        private void lockAspectCheckBox_CheckedChanged(object sender, EventArgs e)
        {

        }

    }
}
